package server.model.administration;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

import server.model.meeting.Announcement;
import server.model.meeting.Meeting;
import server.model.meeting.MeetingInfo;
import server.model.meeting.Participant;
import server.model.meeting.User;
import server.model.meeting.UserInfo;
import server.model.remotes.IMeeting;
import server.model.remotes.IMeetingService;

public class MeetingService extends UnicastRemoteObject implements IMeetingService {

	private ArrayList<Meeting> meetings;
	private Iterator<Meeting> meetingIterator;
	private Admin admin;

	private int meetingCount;
	private ArrayList<Announcement> announcements;
	private ArrayList<Participant> participants;

	
	public MeetingService(Admin admin)throws RemoteException{
		super();
		
		this.admin = admin;
		
		meetingCount = 0;
		meetings = new ArrayList<Meeting>(10);
		meetingIterator = meetings.iterator();
		announcements = new ArrayList<Announcement>(10);	
		participants = new ArrayList<Participant>();
	}
			
	public MeetingInfo[] getMeetingsInfo() throws RemoteException{

		MeetingInfo[] meetingInfos= new MeetingInfo[meetingCount];
		
		int i = 0;
		meetingIterator = meetings.iterator();
		while(meetingIterator.hasNext()){
			Meeting currentMeeting = meetingIterator.next();
			meetingInfos[i] = currentMeeting.getMeetingInfo();
			i++;
		}
		return meetingInfos;
	}
	public IMeeting attendMeeting(String name) throws RemoteException {
		meetingIterator = meetings.iterator();
		while(meetingIterator.hasNext()){	
			
			Meeting currentMeeting = meetingIterator.next();	
			if (name.matches(currentMeeting.getMeetingInfo().getName())){

				return currentMeeting;			
				
			}
		}
		return null;		
	}

	public UserInfo getUserInfo(Number sessionId)
	{
		for(int i = 0; i < participants.size(); i++)
		{
			
		}
	}
	public boolean addMeeting(String name, boolean openMeeting,	String chairman, Date start, Date end, String[] participants)throws RemoteException {
		Meeting meeting = new Meeting();
		MeetingInfo meetingInfo = new MeetingInfo();
		meetingInfo.setName(name);
		meetingInfo.setStartDate(start);
		meetingInfo.setEndDate(end);
		meetingInfo.setOpenMeeting(openMeeting);
		meetingInfo.setChairman((User)admin.getParticipantByName(chairman));
			
		for (int i=0; i<participants.length; i++){		
			meeting.addParticipant(admin.getParticipantByName(participants[i]));
		}
		meeting.setMeetingInfo(meetingInfo);
		meetings.add(meeting);
		meetingCount++;
		return true;			
	}

	@Override
	public ArrayList<Announcement> getAnnouncements() throws RemoteException {
		return announcements;
	}

	@Override
	public void addAnnouncement(Announcement announcement)throws RemoteException {
		announcements.add(announcement);
	}

	@Override
	public ArrayList<Participant> addParticipants(Participant participant)
			throws RemoteException {
		participants.add(participant);
		
		return participants;
	}

}
